home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 1998 November / IRIX 6.5.2 Base Documentation November 1998.img / usr / share / catman / u_man / cat1 / perlsyn.z / perlsyn
Text File  |  1998-10-30  |  27KB  |  793 lines

  1.  
  2.  
  3.  
  4. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      perlsyn - Perl syntax
  10.  
  11. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.      A Perl script consists of a sequence of declarations and statements.  The
  13.      only things that need to be declared in Perl are report formats and
  14.      subroutines.  See the sections below for more information on those
  15.      declarations.  All uninitialized user-created objects are assumed to
  16.      start with a null or 0 value until they are defined by some explicit
  17.      operation such as assignment.  (Though you can get warnings about the use
  18.      of undefined values if you like.)  The sequence of statements is executed
  19.      just once, unlike in sssseeeedddd and aaaawwwwkkkk scripts, where the sequence of
  20.      statements is executed for each input line.  While this means that you
  21.      must explicitly loop over the lines of your input file (or files), it
  22.      also means you have much more control over which files and which lines
  23.      you look at.  (Actually, I'm lying--it is possible to do an implicit loop
  24.      with either the ----nnnn or ----pppp switch.  It's just not the mandatory default
  25.      like it is in sssseeeedddd and aaaawwwwkkkk.)
  26.  
  27.      DDDDeeeeccccllllaaaarrrraaaattttiiiioooonnnnssss
  28.  
  29.      Perl is, for the most part, a free-form language.  (The only exception to
  30.      this is format declarations, for obvious reasons.) Comments are indicated
  31.      by the "#" character, and extend to the end of the line.  If you attempt
  32.      to use /* */ C-style comments, it will be interpreted either as division
  33.      or pattern matching, depending on the context, and C++ // comments just
  34.      look like a null regular expression, so don't do that.
  35.  
  36.      A declaration can be put anywhere a statement can, but has no effect on
  37.      the execution of the primary sequence of statements--declarations all
  38.      take effect at compile time.  Typically all the declarations are put at
  39.      the beginning or the end of the script.  However, if you're using
  40.      lexically-scoped private variables created with _m_y(), you'll have to make
  41.      sure your format or subroutine definition is within the same block scope
  42.      as the my if you expect to be able to access those private variables.
  43.  
  44.      Declaring a subroutine allows a subroutine name to be used as if it were
  45.      a list operator from that point forward in the program.  You can declare
  46.      a subroutine without defining it by saying sub name, thus:
  47.  
  48.          sub myname;
  49.          $me = myname $0             or die "can't get myname";
  50.  
  51.      Note that it functions as a list operator, not as a unary operator; so be
  52.      careful to use or instead of || in this case.  However, if you were to
  53.      declare the subroutine as sub myname ($), then myname would functonion as
  54.      a unary operator, so either or or || would work.
  55.  
  56.      Subroutines declarations can also be loaded up with the require statement
  57.      or both loaded and imported into your namespace with a use statement.
  58.      See the _p_e_r_l_m_o_d manpage for details on this.
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  71.  
  72.  
  73.  
  74.      A statement sequence may contain declarations of lexically-scoped
  75.      variables, but apart from declaring a variable name, the declaration acts
  76.      like an ordinary statement, and is elaborated within the sequence of
  77.      statements as if it were an ordinary statement.  That means it actually
  78.      has both compile-time and run-time effects.
  79.  
  80.      SSSSiiiimmmmpppplllleeee ssssttttaaaatttteeeemmmmeeeennnnttttssss
  81.  
  82.      The only kind of simple statement is an expression evaluated for its side
  83.      effects.  Every simple statement must be terminated with a semicolon,
  84.      unless it is the final statement in a block, in which case the semicolon
  85.      is optional.  (A semicolon is still encouraged there if the block takes
  86.      up more than one line, because you may eventually add another line.)
  87.      Note that there are some operators like eval {} and do {} that look like
  88.      compound statements, but aren't (they're just TERMs in an expression),
  89.      and thus need an explicit termination if used as the last item in a
  90.      statement.
  91.  
  92.      Any simple statement may optionally be followed by a _S_I_N_G_L_E modifier,
  93.      just before the terminating semicolon (or block ending).  The possible
  94.      modifiers are:
  95.  
  96.          if EXPR
  97.          unless EXPR
  98.          while EXPR
  99.          until EXPR
  100.  
  101.      The if and unless modifiers have the expected semantics, presuming you're
  102.      a speaker of English.  The while and until modifiers also have the usual
  103.      "while loop" semantics (conditional evaluated first), except when applied
  104.      to a do-BLOCK (or to the now-deprecated do-SUBROUTINE statement), in
  105.      which case the block executes once before the conditional is evaluated.
  106.      This is so that you can write loops like:
  107.  
  108.          do {
  109.              $line = <STDIN>;
  110.              ...
  111.          } until $line  eq ".\n";
  112.  
  113.      See the do entry in the _p_e_r_l_f_u_n_c manpage.  Note also that the loop
  114.      control statements described later will _N_O_T work in this construct,
  115.      because modifiers don't take loop labels.  Sorry.  You can always wrap
  116.      another block around it to do that sort of thing.
  117.  
  118.      CCCCoooommmmppppoooouuuunnnndddd ssssttttaaaatttteeeemmmmeeeennnnttttssss
  119.  
  120.      In Perl, a sequence of statements that defines a scope is called a block.
  121.      Sometimes a block is delimited by the file containing it (in the case of
  122.      a required file, or the program as a whole), and sometimes a block is
  123.      delimited by the extent of a string (in the case of an eval).
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  137.  
  138.  
  139.  
  140.      But generally, a block is delimited by curly brackets, also known as
  141.      braces.  We will call this syntactic construct a BLOCK.
  142.  
  143.      The following compound statements may be used to control flow:
  144.  
  145.          if (EXPR) BLOCK
  146.          if (EXPR) BLOCK else BLOCK
  147.          if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  148.          LABEL while (EXPR) BLOCK
  149.          LABEL while (EXPR) BLOCK continue BLOCK
  150.          LABEL for (EXPR; EXPR; EXPR) BLOCK
  151.          LABEL foreach VAR (LIST) BLOCK
  152.          LABEL BLOCK continue BLOCK
  153.  
  154.      Note that, unlike C and Pascal, these are defined in terms of BLOCKs, not
  155.      statements.  This means that the curly brackets are _r_e_q_u_i_r_e_d--no dangling
  156.      statements allowed.  If you want to write conditionals without curly
  157.      brackets there are several other ways to do it.  The following all do the
  158.      same thing:
  159.  
  160.          if (!open(FOO)) { die "Can't open $FOO: $!"; }
  161.          die "Can't open $FOO: $!" unless open(FOO);
  162.          open(FOO) or die "Can't open $FOO: $!";     # FOO or bust!
  163.          open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
  164.                              # a bit exotic, that last one
  165.  
  166.      The if statement is straightforward.  Because BLOCKs are always bounded
  167.      by curly brackets, there is never any ambiguity about which if an else
  168.      goes with.  If you use unless in place of if, the sense of the test is
  169.      reversed.
  170.  
  171.      The while statement executes the block as long as the expression is true
  172.      (does not evaluate to the null string or 0 or "0").  The LABEL is
  173.      optional, and if present, consists of an identifier followed by a colon.
  174.      The LABEL identifies the loop for the loop control statements next, last,
  175.      and redo.  If the LABEL is omitted, the loop control statement refers to
  176.      the innermost enclosing loop.  This may include dynamically looking back
  177.      your call-stack at run time to find the LABEL.  Such desperate behavior
  178.      triggers a warning if you use the ----wwww flag.
  179.  
  180.      If there is a continue BLOCK, it is always executed just before the
  181.      conditional is about to be evaluated again, just like the third part of a
  182.      for loop in C.  Thus it can be used to increment a loop variable, even
  183.      when the loop has been continued via the next statement (which is similar
  184.      to the C continue statement).
  185.  
  186.      LLLLoooooooopppp CCCCoooonnnnttttrrrroooollll
  187.  
  188.      The next command is like the continue statement in C; it starts the next
  189.      iteration of the loop:
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  203.  
  204.  
  205.  
  206.          LINE: while (<STDIN>) {
  207.              next LINE if /^#/;      # discard comments
  208.              ...
  209.          }
  210.  
  211.      The last command is like the break statement in C (as used in loops); it
  212.      immediately exits the loop in question.  The continue block, if any, is
  213.      not executed:
  214.  
  215.          LINE: while (<STDIN>) {
  216.              last LINE if /^$/;      # exit when done with header
  217.              ...
  218.          }
  219.  
  220.      The redo command restarts the loop block without evaluating the
  221.      conditional again.  The continue block, if any, is _n_o_t executed.  This
  222.      command is normally used by programs that want to lie to themselves about
  223.      what was just input.
  224.  
  225.      For example, when processing a file like /_e_t_c/_t_e_r_m_c_a_p.  If your input
  226.      lines might end in backslashes to indicate continuation, you want to skip
  227.      ahead and get the next record.
  228.  
  229.          while (<>) {
  230.              chomp;
  231.              if (s/\\$//) {
  232.                  $_ .= <>;
  233.                  redo unless eof();
  234.              }
  235.              # now process $_
  236.          }
  237.  
  238.      which is Perl short-hand for the more explicitly written version:
  239.  
  240.          LINE: while (defined($line = <ARGV>)) {
  241.              chomp($line);
  242.              if ($line =~ s/\\$//) {
  243.                  $line .= <ARGV>;
  244.                  redo LINE unless eof(); # not eof(ARGV)!
  245.              }
  246.              # now process $line
  247.          }
  248.  
  249.      Or here's a simpleminded Pascal comment stripper (warning: assumes no {
  250.      or } in strings).
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  269.  
  270.  
  271.  
  272.          LINE: while (<STDIN>) {
  273.              while (s|({.*}.*){.*}|$1 |) {}
  274.              s|{.*}| |;
  275.              if (s|{.*| |) {
  276.                  $front = $_;
  277.                  while (<STDIN>) {
  278.                      if (/}/) {      # end of comment?
  279.                          s|^|$front{|;
  280.                          redo LINE;
  281.                      }
  282.                  }
  283.              }
  284.              print;
  285.          }
  286.  
  287.      Note that if there were a continue block on the above code, it would get
  288.      executed even on discarded lines.
  289.  
  290.      If the word while is replaced by the word until, the sense of the test is
  291.      reversed, but the conditional is still tested before the first iteration.
  292.  
  293.      The form while/if BLOCK BLOCK, available in Perl 4, is no longer
  294.      available.   Replace any occurrence of if BLOCK by if (do BLOCK).
  295.  
  296.      FFFFoooorrrr LLLLooooooooppppssss
  297.  
  298.      Perl's C-style for loop works exactly like the corresponding while loop;
  299.      that means that this:
  300.  
  301.          for ($i = 1; $i < 10; $i++) {
  302.              ...
  303.          }
  304.  
  305.      is the same as this:
  306.  
  307.          $i = 1;
  308.          while ($i < 10) {
  309.              ...
  310.          } continue {
  311.              $i++;
  312.          }
  313.  
  314.      (There is one minor difference: The first form implies a lexical scope
  315.      for variables declared with my in the initialization expression.)
  316.  
  317.      Besides the normal array index looping, for can lend itself to many other
  318.      interesting applications.  Here's one that avoids the problem you get
  319.      into if you explicitly test for end-of-file on an interactive file
  320.      descriptor causing your program to appear to hang.
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  335.  
  336.  
  337.  
  338.          $on_a_tty = -t STDIN && -t STDOUT;
  339.          sub prompt { print "yes? " if $on_a_tty }
  340.          for ( prompt(); <STDIN>; prompt() ) {
  341.              # do something
  342.          }
  343.  
  344.  
  345.      FFFFoooorrrreeeeaaaacccchhhh LLLLooooooooppppssss
  346.  
  347.      The foreach loop iterates over a normal list value and sets the variable
  348.      VAR to be each element of the list in turn.  If the variable is preceded
  349.      with the keyword my, then it is lexically scoped, and is therefore
  350.      visible only within the loop.  Otherwise, the variable is implicitly
  351.      local to the loop and regains its former value upon exiting the loop.  If
  352.      the variable was previously declared with my, it uses that variable
  353.      instead of the global one, but it's still localized to the loop.  (Note
  354.      that a lexically scoped variable can cause problems with you have
  355.      subroutine or format declarations.)
  356.  
  357.      The foreach keyword is actually a synonym for the for keyword, so you can
  358.      use foreach for readability or for for brevity.  If VAR is omitted, $_ is
  359.      set to each value.  If LIST is an actual array (as opposed to an
  360.      expression returning a list value), you can modify each element of the
  361.      array by modifying VAR inside the loop.  That's because the foreach loop
  362.      index variable is an implicit alias for each item in the list that you're
  363.      looping over.
  364.  
  365.      Examples:
  366.  
  367.          for (@ary) { s/foo/bar/ }
  368.  
  369.          foreach my $elem (@elements) {
  370.              $elem *= 2;
  371.          }
  372.  
  373.          for $count (10,9,8,7,6,5,4,3,2,1,'BOOM') {
  374.              print $count, "\n"; sleep(1);
  375.          }
  376.  
  377.          for (1..15) { print "Merry Christmas\n"; }
  378.  
  379.          foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) {
  380.              print "Item: $item\n";
  381.          }
  382.  
  383.      Here's how a C programmer might code up a particular algorithm in Perl:
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  401.  
  402.  
  403.  
  404.          for (my $i = 0; $i < @ary1; $i++) {
  405.              for (my $j = 0; $j < @ary2; $j++) {
  406.                  if ($ary1[$i] > $ary2[$j]) {
  407.                      last; # can't go to outer :-(
  408.                  }
  409.                  $ary1[$i] += $ary2[$j];
  410.              }
  411.              # this is where that last takes me
  412.          }
  413.  
  414.      Whereas here's how a Perl programmer more comfortable with the idiom
  415.      might do it:
  416.  
  417.          OUTER: foreach my $wid (@ary1) {
  418.          INNER:   foreach my $jet (@ary2) {
  419.                      next OUTER if $wid > $jet;
  420.                      $wid += $jet;
  421.                   }
  422.                }
  423.  
  424.      See how much easier this is?  It's cleaner, safer, and faster.  It's
  425.      cleaner because it's less noisy.  It's safer because if code gets added
  426.      between the inner and outer loops later on, the new code won't be
  427.      accidentally executed.  The next explicitly iterates the other loop
  428.      rather than merely terminating the inner one.  And it's faster because
  429.      Perl executes a foreach statement more rapidly than it would the
  430.      equivalent for loop.
  431.  
  432.      BBBBaaaassssiiiicccc BBBBLLLLOOOOCCCCKKKKssss aaaannnndddd SSSSwwwwiiiittttcccchhhh SSSSttttaaaatttteeeemmmmeeeennnnttttssss
  433.  
  434.      A BLOCK by itself (labeled or not) is semantically equivalent to a loop
  435.      that executes once.  Thus you can use any of the loop control statements
  436.      in it to leave or restart the block.  (Note that this is _N_O_T true in
  437.      eval{}, sub{}, or contrary to popular belief do{} blocks, which do _N_O_T
  438.      count as loops.)  The continue block is optional.
  439.  
  440.      The BLOCK construct is particularly nice for doing case structures.
  441.  
  442.          SWITCH: {
  443.              if (/^abc/) { $abc = 1; last SWITCH; }
  444.              if (/^def/) { $def = 1; last SWITCH; }
  445.              if (/^xyz/) { $xyz = 1; last SWITCH; }
  446.              $nothing = 1;
  447.          }
  448.  
  449.      There is no official switch statement in Perl, because there are already
  450.      several ways to write the equivalent.  In addition to the above, you
  451.      could write
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  467.  
  468.  
  469.  
  470.          SWITCH: {
  471.              $abc = 1, last SWITCH  if /^abc/;
  472.              $def = 1, last SWITCH  if /^def/;
  473.              $xyz = 1, last SWITCH  if /^xyz/;
  474.              $nothing = 1;
  475.          }
  476.  
  477.      (That's actually not as strange as it looks once you realize that you can
  478.      use loop control "operators" within an expression,  That's just the
  479.      normal C comma operator.)
  480.  
  481.      or
  482.  
  483.          SWITCH: {
  484.              /^abc/ && do { $abc = 1; last SWITCH; };
  485.              /^def/ && do { $def = 1; last SWITCH; };
  486.              /^xyz/ && do { $xyz = 1; last SWITCH; };
  487.              $nothing = 1;
  488.          }
  489.  
  490.      or formatted so it stands out more as a "proper" switch statement:
  491.  
  492.          SWITCH: {
  493.              /^abc/      && do {
  494.                                  $abc = 1;
  495.                                  last SWITCH;
  496.                             };
  497.  
  498.              /^def/      && do {
  499.                                  $def = 1;
  500.                                  last SWITCH;
  501.                             };
  502.  
  503.              /^xyz/      && do {
  504.                                  $xyz = 1;
  505.                                  last SWITCH;
  506.                              };
  507.              $nothing = 1;
  508.          }
  509.  
  510.      or
  511.  
  512.          SWITCH: {
  513.              /^abc/ and $abc = 1, last SWITCH;
  514.              /^def/ and $def = 1, last SWITCH;
  515.              /^xyz/ and $xyz = 1, last SWITCH;
  516.              $nothing = 1;
  517.          }
  518.  
  519.      or even, horrors,
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  533.  
  534.  
  535.  
  536.          if (/^abc/)
  537.              { $abc = 1 }
  538.          elsif (/^def/)
  539.              { $def = 1 }
  540.          elsif (/^xyz/)
  541.              { $xyz = 1 }
  542.          else
  543.              { $nothing = 1 }
  544.  
  545.      A common idiom for a switch statement is to use foreach's aliasing to
  546.      make a temporary assignment to $_ for convenient matching:
  547.  
  548.          SWITCH: for ($where) {
  549.                      /In Card Names/     && do { push @flags, '-e'; last; };
  550.                      /Anywhere/          && do { push @flags, '-h'; last; };
  551.                      /In Rulings/        && do {                    last; };
  552.                      die "unknown value for form variable where: `$where'";
  553.                  }
  554.  
  555.      Another interesting approach to a switch statement is arrange for a do
  556.      block to return the proper value:
  557.  
  558.          $amode = do {
  559.              if     ($flag & O_RDONLY) { "r" }
  560.              elsif  ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
  561.              elsif  ($flag & O_RDWR)   {
  562.                  if ($flag & O_CREAT)  { "w+" }
  563.                  else                  { ($flag & O_APPEND) ? "a+" : "r+" }
  564.              }
  565.          };
  566.  
  567.  
  568.      GGGGoooottttoooo
  569.  
  570.      Although not for the faint of heart, Perl does support a goto statement.
  571.      A loop's LABEL is not actually a valid target for a goto; it's just the
  572.      name of the loop.  There are three forms: goto-LABEL, goto-EXPR, and
  573.      goto-&NAME.
  574.  
  575.      The goto-LABEL form finds the statement labeled with LABEL and resumes
  576.      execution there.  It may not be used to go into any construct that
  577.      requires initialization, such as a subroutine or a foreach loop.  It also
  578.      can't be used to go into a construct that is optimized away.  It can be
  579.      used to go almost anywhere else within the dynamic scope, including out
  580.      of subroutines, but it's usually better to use some other construct such
  581.      as last or die.  The author of Perl has never felt the need to use this
  582.      form of goto (in Perl, that is--C is another matter).
  583.  
  584.      The goto-EXPR form expects a label name, whose scope will be resolved
  585.      dynamically.  This allows for computed gotos per FORTRAN, but isn't
  586.      necessarily recommended if you're optimizing for maintainability:
  587.  
  588.  
  589.  
  590.  
  591.                                                                         PPPPaaaaggggeeee 9999
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  599.  
  600.  
  601.  
  602.          goto ("FOO", "BAR", "GLARCH")[$i];
  603.  
  604.      The goto-&NAME form is highly magical, and substitutes a call to the
  605.      named subroutine for the currently running subroutine.  This is used by
  606.      _A_U_T_O_L_O_A_D() subroutines that wish to load another subroutine and then
  607.      pretend that the other subroutine had been called in the first place
  608.      (except that any modifications to @_ in the current subroutine are
  609.      propagated to the other subroutine.)  After the goto, not even _c_a_l_l_e_r()
  610.      will be able to tell that this routine was called first.
  611.  
  612.      In almost all cases like this, it's usually a far, far better idea to use
  613.      the structured control flow mechanisms of next, last, or redo instead of
  614.      resorting to a goto.  For certain applications, the catch and throw pair
  615.      of eval{} and _d_i_e() for exception processing can also be a prudent
  616.      approach.
  617.  
  618.      PPPPOOOODDDDssss:::: EEEEmmmmbbbbeeeeddddddddeeeedddd DDDDooooccccuuuummmmeeeennnnttttaaaattttiiiioooonnnn
  619.  
  620.      Perl has a mechanism for intermixing documentation with source code.
  621.      While it's expecting the beginning of a new statement, if the compiler
  622.      encounters a line that begins with an equal sign and a word, like this
  623.  
  624.          =head1 Here There Be Pods!
  625.  
  626.      Then that text and all remaining text up through and including a line
  627.      beginning with =cut will be ignored.  The format of the intervening text
  628.      is described in the _p_e_r_l_p_o_d manpage.
  629.  
  630.      This allows you to intermix your source code and your documentation text
  631.      freely, as in
  632.  
  633.          =item snazzle($)
  634.  
  635.          The snazzle() function will behave in the most spectacular
  636.          form that you can possibly imagine, not even excepting
  637.          cybernetic pyrotechnics.
  638.  
  639.          =cut back to the compiler, nuff of this pod stuff!
  640.  
  641.          sub snazzle($) {
  642.              my $thingie = shift;
  643.              .........
  644.          }
  645.  
  646.      Note that pod translators should look at only paragraphs beginning with a
  647.      pod directive (it makes parsing easier), whereas the compiler actually
  648.      knows to look for pod escapes even in the middle of a paragraph.  This
  649.      means that the following secret stuff will be ignored by both the
  650.      compiler and the translators.
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                                                        PPPPaaaaggggeeee 11110000
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  665.  
  666.  
  667.  
  668.          $a=3;
  669.          =secret stuff
  670.           warn "Neither POD nor CODE!?"
  671.          =cut back
  672.          print "got $a\n";
  673.  
  674.      You probably shouldn't rely upon the _w_a_r_n() being podded out forever.
  675.      Not all pod translators are well-behaved in this regard, and perhaps the
  676.      compiler will become pickier.
  677.  
  678.      One may also use pod directives to quickly comment out a section of code.
  679.  
  680.      PPPPllllaaaaiiiinnnn OOOOlllldddd CCCCoooommmmmmmmeeeennnnttttssss ((((NNNNooootttt!!!!))))
  681.  
  682.      Much like the C preprocessor, perl can process line directives.  Using
  683.      this, one can control perl's idea of filenames and line numbers in error
  684.      or warning messages (especially for strings that are processed with
  685.      _e_v_a_l()).  The syntax for this mechanism is the same as for most C
  686.      preprocessors: it matches the regular expression
  687.      /^#\s*line\s+(\d+)\s*(?:\s"([^"]*)")?/ with $1 being the line number for
  688.      the next line, and $2 being the optional filename (specified within
  689.      quotes).
  690.  
  691.      Here are some examples that you should be able to type into your command
  692.      shell:
  693.  
  694.          % perl
  695.          # line 200 "bzzzt"
  696.          # the `#' on the previous line must be the first char on line
  697.          die 'foo';
  698.          __END__
  699.          foo at bzzzt line 201.
  700.  
  701.          % perl
  702.          # line 200 "bzzzt"
  703.          eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
  704.          __END__
  705.          foo at - line 2001.
  706.  
  707.          % perl
  708.          eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
  709.          __END__
  710.          foo at foo bar line 200.
  711.  
  712.          % perl
  713.          # line 345 "goop"
  714.          eval "\n#line " . __LINE__ . ' "' . __FILE__ ."\"\ndie 'foo'";
  715.          print $@;
  716.          __END__
  717.          foo at goop line 345.
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                                                        PPPPaaaaggggeeee 11111111
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))                                                          PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.                                                                        PPPPaaaaggggeeee 11112222
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.